Skip to contentMethod: bind(BindingGroup, Object, JTable)
1: /*
2: * *********************************************************************************************************************
3: *
4: * TheseFoolishThings: Miscellaneous utilities
5: * http://tidalwave.it/projects/thesefoolishthings
6: *
7: * Copyright (C) 2009 - 2023 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
23: * git clone https://github.com/tidalwave-it/thesefoolishthings-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.thesefoolishthings.examples.dci.swing.swing;
28:
29: import javax.annotation.Nonnull;
30: import java.util.List;
31: import javax.swing.JList;
32: import javax.swing.JTable;
33: import org.jdesktop.beansbinding.BeanProperty;
34: import org.jdesktop.beansbinding.BindingGroup;
35: import org.jdesktop.observablecollections.ObservableList;
36: import org.jdesktop.swingbinding.JTableBinding;
37: import org.jdesktop.swingbinding.SwingBindings;
38: import it.tidalwave.util.AsExtensions;
39: import it.tidalwave.thesefoolishthings.examples.dci.swing.role.ObservableListProvider;
40: import it.tidalwave.thesefoolishthings.examples.dci.swing.role.TableHeaderDescriptor;
41: import lombok.AccessLevel;
42: import lombok.NoArgsConstructor;
43: import lombok.experimental.ExtensionMethod;
44: import static it.tidalwave.thesefoolishthings.examples.dci.swing.role.ObservableListProvider._ObservableListProvider_;
45: import static it.tidalwave.thesefoolishthings.examples.dci.swing.role.TableHeaderDescriptor._TableHeaderDescriptor_;
46: import static org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.*;
47:
48: /***********************************************************************************************************************
49: *
50: * A facility to bind some Swing components to data.
51: *
52: * @author Fabrizio Giudici
53: *
54: **********************************************************************************************************************/
55: @ExtensionMethod(AsExtensions.class)
56: @NoArgsConstructor(access = AccessLevel.PRIVATE)
57: public final class Bindings
58: {
59: /*******************************************************************************************************************
60: *
61: * Binds a source to a {@link JList}. Two roles are used:
62: *
63: * <ol>
64: * <li>{@link ObservableListProvider} to retrieve the list model</li>
65: * <li>{@link HtmlRenderableListCellRenderer} to retrieve rendering for each item</li>
66: * </ol>
67: *
68: * @param bindings the {@link BindingGroup} to add the new binding to
69: * @param datum the datum
70: * @param jList the {@code JList}
71: *
72: ******************************************************************************************************************/
73: public static void bind (@Nonnull final BindingGroup bindings,
74: @Nonnull final Object datum,
75: @Nonnull final JList jList)
76: {
77: final ObservableList<?> ol = datum.as(_ObservableListProvider_).createObservableList();
78: bindings.addBinding(SwingBindings.createJListBinding(READ, ol, jList));
79: jList.setCellRenderer(new HtmlRenderableListCellRenderer());
80: }
81:
82: /*******************************************************************************************************************
83: *
84: * Binds a source to a {@link JTable}. Two roles are used:
85: *
86: * <ol>
87: * <li>{@link ObservableListProvider} to retrieve the list model</li>
88: * <li>{@link TableHeaderDescriptor} to retrieve the column definition</li>
89: * </ol>
90: *
91: * @param bindings the {@link BindingGroup} to add the new binding to
92: * @param datum the datum
93: * @param jTable the {@code JTable}
94: *
95: ******************************************************************************************************************/
96: public static <T> void bind (@Nonnull final BindingGroup bindings,
97: @Nonnull final Object datum,
98: @Nonnull final JTable jTable)
99: {
100: final ObservableList<T> ol = datum.as(_ObservableListProvider_).createObservableList();
101: final JTableBinding<T, List<T>, JTable> tb = SwingBindings.createJTableBinding(READ_WRITE, ol, jTable);
102:
103: datum.as(_TableHeaderDescriptor_).getColumnDescriptors().forEach(cd ->
104: tb.addColumnBinding(BeanProperty.create(cd.getPropertyName())).setColumnName(cd.getHeaderName()));
105:
106: bindings.addBinding(tb);
107: }
108: }